package com.cs.dingdingauto.extent

import android.content.Context
import android.os.Build
import java.lang.reflect.Method

object WindowUtils {

    /**
     * 展开通知栏
     * @param context
     */
    @JvmStatic
    fun expandNotification(context: Context) {
        val service = context.getSystemService("statusbar") ?: return
        try {
            val clazz = Class.forName("android.app.StatusBarManager")
            val sdkVersion = Build.VERSION.SDK_INT
            var expand: Method? = null
            expand = if (sdkVersion <= 16) {
                clazz.getDeclaredMethod("expand")
            } else {
                /*
                    * Android SDK 16之后的版本展开通知栏有两个接口可以处理
                    * expandNotificationsPanel()
                    * expandSettingsPanel()
                    */
                clazz.getMethod("expandNotificationsPanel");
//                clazz.getDeclaredMethod("expandSettingsPanel")
            }
            expand.isAccessible = true
            expand.invoke(service)
        } catch (e: Exception) {
            e.printStackTrace();
        }
    }

    @JvmStatic
    fun collapseStatusBar(context: Context) {
        val service = context.getSystemService("statusbar") ?: return
        try {
            val clazz = Class.forName("android.app.StatusBarManager")
            val sdkVersion = Build.VERSION.SDK_INT
            var collapse: Method? = null
            collapse = if (sdkVersion <= 16) {
                clazz.getMethod("collapse")
            } else {
                clazz.getMethod("collapsePanels")
            }
            collapse.isAccessible = true
            collapse.invoke(service)
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }
}